import {createDiscreteApi} from "naive-ui";
import {useRenderIcon} from "@/utils/soul";
import useSettingsStore from "@/stores/admin/useSettingStore";

export function musicToAudit(music){
    let audio = {}
    if (music){
        audio = {
            id:music.id,
            title: music.musicName,
            name: music.musicName,
            url: music.musicFile?.fileUrl,
            cover: music.picUrl, // prettier-ignore
            artist: music.singerVo?.name || '未知',
            src: music.musicFile?.fileUrl,
            pic: music.picUrl,
        }
    }
    return audio;
}

export function musicToAuditList(musicList){
    const audios = []
    if (musicList && musicList.length > 0){
        musicList.forEach(m=>{
            let audit = musicToAudit(m);
            if (audit){
                audios.push(audit);
            }
        })
    }
    return audios;
}

export const getUrlParams = (info) => {
    let str = ""
    if (info) {
        for (let key in info) {
            let v = info[key] == null || info[key] == undefined ? "" : encodeURIComponent(info[key]);
            if (str != "") {
                str += `&${key}=${v}`
            } else {
                str += `${key}=${v}`
            }
        }
    }
    return str
}
export const spliceUrlParams = (url, info) => {
    let paramstr = getUrlParams(info);
    return url + (paramstr ? (url.indexOf("?") > -1 ? "&" : "?") + paramstr : "");
}
/**
 * 路由转菜单对象
 * @returns {*}
 * @param routeTree
 */
export const getMenuOptions = (routeTree )  =>{
    return routeTree.map( route=> {
        let menu = {
            key : route.name,
            label: route.meta?.title,
            show: !route.hidden
        }
        if (route.meta?.icon ) {
            menu.icon = useRenderIcon(route.meta?.icon)
        }
        if (route.children && route.children.length > 0){
            let menuOptions = getMenuOptions(route.children);
            if (!route.meta ){
                if (menuOptions.length > 0){
                    return menuOptions[0]
                }
                menu.hidden = true
            }
            menu.children = menuOptions;
        }else {
            delete menu.children
        }
        return menu;
    })
}
/**
 * 简单实现对象的深度克隆
 * 注意：
 *      1、使用该方式会出现一些问题，如值为undefined、函数、Date类型等属性会无法被克隆，此处待完善！！！
 *      2、非得使用undefined的话，可以使用null替代
 * @param obj
 */
export function useDeepClone(obj ) {
    // 如果是基本数据类型，直接返回
    if (typeof obj !== 'object' || obj === null) {
        return obj;
    }
    // 根据类型创建新对象或数组
    const newObj = Array.isArray(obj) ? [] : {};
    // 递归复制属性或元素
    for (const key in obj) {
        newObj[key] = useDeepClone(obj[key]);
    }
    return newObj;
}
/**
 * 反馈组件：通知弹框
 */
export const naive  = {
    message: {},
    dialog: {},
    notification: {},
    loading:{},
}

export const loadingNaiveUI = ()=>{
    const {getTheme, themeColor} = storeToRefs(useSettingsStore())
    const configProviderPropsRef = computed(() => ({
        theme: getTheme.value,
        themeOverrides: {
            common: {
                primaryColor: themeColor?.value,
                // primaryColorHover: useLighten(themeColor?.value as string, 6),
                // primaryColorPressed: useLighten(themeColor?.value as string, 6),
            },
            LoadingBar: {
                colorLoading: themeColor?.value,
            },
        },
    }));
    const notificationProviderProps =  {
        placement:"bottom-right",
        keepAliveOnHover:true,
    };
    const {message, dialog, notification, loadingBar} = createDiscreteApi(
        ["message", "dialog", "notification", "loadingBar"],
        {
            configProviderProps: configProviderPropsRef,
            notificationProviderProps:notificationProviderProps
        }
    );

    naive.message=message;
    naive.dialog=dialog;
    naive.notification=notification;
    naive.loading=loadingBar;
}
